extends Node

# 添加新的统计信息时要注意的事项：
# 1. 声明变量
# 2. 在`reset_player_stats()`中设置重置时的状态
# 3. 保存或加载变量时的逻辑

signal changed_hp(amount)
signal player_dead

enum Abilities {
	DOUBLE_JUMP,
	DASH,
	WALL_JUMP,
}

var playerRespawnPoints := []
var player_interacting_with := []

var GlobalEvents := []
var player_abilities: Array[Abilities] = []
var player_direction: int

var player_coin: int = 0
var collected_coins := []

var highest_level: int = 0

@export var PLAYER_MAX_HP := 10.0

var player_HP := PLAYER_MAX_HP:
	set(v):
		if v < PLAYER_MAX_HP:
			player_HP = v
		else:
			player_HP = PLAYER_MAX_HP

var dialog_showing_list := []


func reset_player_stats() -> void:
	player_HP = PLAYER_MAX_HP
	playerRespawnPoints = []
	player_abilities = []
	GlobalEvents = []
	collected_coins = []
	player_coin = 0
	highest_level = 0


func change_hp(amount: float) -> int:
	# return code:
	# -1: player is dead
	# 0: negative change
	# 1: positive change
	
	player_HP += amount
	changed_hp.emit(amount)
	if player_HP <= 0:
		player_dead.emit()
		return -1
	
	return 0 if amount < 0 else 1


func change_coin(amount: int) -> void:
	player_coin += amount


func reach_level(num: int) -> void:
	if num > highest_level:
		highest_level = num


func to_dict() -> Dictionary:
	return {
		global_events = GlobalEvents,
		player_abilities = player_abilities,
		player_max_hp = PLAYER_MAX_HP,
		player_hp = player_HP,
		player_coin = player_coin,
		collected_coins = collected_coins,
		highest_level = highest_level,
	}


func from_dict(dict: Dictionary) -> void:
	GlobalEvents = dict.global_events
	PLAYER_MAX_HP = dict.player_max_hp
	player_HP = dict.player_hp
	player_coin = dict.player_coin
	collected_coins = dict.collected_coins
	highest_level = dict.highest_level
	
	var temp_abilities := dict.player_abilities as Array
	player_abilities = []
	for ability in temp_abilities:
		player_abilities.append(int(ability))
